package com.greenantmall.activity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.avos.avoscloud.AVUser;
import com.greenantmall.AntHomeApplication;
import com.greenantmall.R;
import com.greenantmall.adapter.ShoppingCartDeleteListAdapter;
import com.greenantmall.adapter.ShoppingCartListAdapter;
import com.greenantmall.adapter.ShoppingCartListAdapter.ShoppingCartItemData;
import com.greenantmall.adapter.ShoppingCartListAdapter.ShoppingCartListAdapterListener;
import com.greenantmall.bean.ServerSection;
import com.greenantmall.bean.ShoppingCart;
import com.greenantmall.bean.StoreInfo;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.view.ChooseDateDialog;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lsarah.utils.SystemBarTintManager;

public class ShoppingCartActivity extends BaseActivity implements OnRefreshListener<ListView>{
	private View mLayoutContent;
	private View mBottomLayoutNormal;
	private View mBottomLayoutDelete;
	private View mWeightTag;
	private ProgressBar mProgressBar;
	private Button mBtnBottomDelete;
	private Button mBtnBack;
	private Button mBtnCheckAll;
	private Button mBtnAppoinment;
	private Button mBtnSettlement;
	private TextView mTvPrompt;
	private TextView mTvTopTitle;
	private TextView mTvTopEdit;
	private TextView mTvTotalMoney;
	private TextView mTvTotalMoneyWaitWeight;
	private TextView mTvTotalDes;
	private PullToRefreshListView mPullToRefreshListView;
	
	private StoreInfo mCvsStoreInfo;
	private List<ShoppingCartItemData> mDataList;
	private List<ShoppingCartItemData> mDataListForDelete;
	private ShoppingCartListAdapter mShoppingCartListAdapter;
	private ShoppingCartDeleteListAdapter mShoppingCartDeleteListAdapter;
	
	private double mTotalMoney = 0;
	
	private DecimalFormat mDecimalFormat;
	
	private Calendar mOpenCalendar;
	private Calendar mCloseCalendar;
	private ServerSection mServerSection;
	private boolean isEditMode = false;
	
	public static final String KEY_SP_SHOPPING_CART = "key_sp_shopping_cart";
	public static final String KEY_STORE_ID = "key_store_id";
	
	private OnClickListener mOnClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
		    if(v.equals(mBtnBack)) {
		        ShoppingCartActivity.this.finish();
		    }
		    else if(v.equals(mBtnAppoinment)) {
		    	ChooseDateDialog dialog = new ChooseDateDialog(ShoppingCartActivity.this, mOpenCalendar, mCloseCalendar);
		    	dialog.show();
		    }
		    else if(v.equals(mBtnSettlement)) {
		    	if(!ShoppingCartActivity.this.isOpen()) {
		    		UtilTools.showMyToast(ShoppingCartActivity.this, 
		    				getResources().getString(R.string.shopping_cart_out_of_send_time));
		    	}
		    	else {
		    		goToOrderCreateActivity(null);
		    	}
		    }
		    else if(v.equals(mBtnCheckAll)) {
		        for(ShoppingCartItemData itemData : mDataList)
	            {
		        	if(itemData.isOffShelf) {
	    				continue;
	    			}
		        	
	                itemData.isChecked = !mBtnCheckAll.isSelected();
	            }
	            mShoppingCartListAdapter.notifyDataSetChanged();
		    }
		    else if(v.equals(mTvTopEdit)) {
		    	changeDisplayMode();
		    }
		    else if(v.equals(mBtnBottomDelete)) {
		    	if(isEditMode) {
		    		for(ShoppingCartItemData itemData : mDataListForDelete) {
		    			if(itemData.isChecked && itemData.itemType == 0) {
		    				final ShoppingCartItemData shoppingCartItemData = itemData;
		    				RetrieveDataAsyn.getInstance().removeShoppingCart(
		    						itemData.mUIShoppingCart, 
		    						-1, 
		    						new RetrieveDataListener() {
								@Override
								public void dataRetrieved(int requestId, Object data) {
									if(data != null) {
										mDataList.remove(shoppingCartItemData);
										mDataListForDelete.remove(shoppingCartItemData);
										refreshDataAfterDelete();
										mShoppingCartDeleteListAdapter.notifyDataSetChanged();
		                                sendBroadcast(new Intent(Const.SHOPPING_CART_CHANGED));
									}
								}
							});
		    			}
		    		}
		    	}
		    }
		}
	};
	
	private RetrieveDataListener mRetrieveDataListener = new RetrieveDataListener()
    {
        @Override
        public void dataRetrieved(int requestId, Object data)
        {
        	AntHomeApplication.shoppingMap.clear();
        	List<ShoppingCart> shoppingCartList = (List<ShoppingCart>) data;
			if (shoppingCartList != null) {
				for (ShoppingCart shoppingCart : shoppingCartList) {
					AntHomeApplication.shoppingMap.put(shoppingCart.getGoodsid(), shoppingCart);
				}
			}
			Intent intent = new Intent(Const.SHOPPING_CART_CHANGED);
			sendBroadcast(intent);
        	
        	mPullToRefreshListView.onRefreshComplete();
        	mLayoutContent.setVisibility(View.VISIBLE);
            mDataList.clear();
            if(data != null) {
                List<ShoppingCart> scList = (List<ShoppingCart>) data;
                arrangeData(scList);
                
                if(mDataList.size() == 0) {
                	//no goods in shopping cart
            		mTvPrompt.setVisibility(View.VISIBLE);
            		mProgressBar.setVisibility(View.GONE);
            		mTvPrompt.setText(getResources().getString(R.string.shopping_cart_no_goods));
                }
                else {
            		mTvPrompt.setVisibility(View.GONE);
            		mProgressBar.setVisibility(View.GONE);
                }
            }
            else {
        		mTvPrompt.setVisibility(View.VISIBLE);
        		mProgressBar.setVisibility(View.GONE);
        		mTvPrompt.setText(getResources().getString(R.string.shopping_cart_data_load_error));
            }
        }
    };
    
    private DataSetObserver mDataSetObserver = new DataSetObserver() {
		@Override
		public void onChanged() {
			super.onChanged();
			refreshTopCount();
		}

		@Override
		public void onInvalidated() {
			super.onInvalidated();
			refreshTopCount();
		}
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		SystemBarTintManager.setContentView(this, R.layout.shopping_cart, getResources().getColor(R.color.green));
		mLayoutContent = findViewById(R.id.shopping_cart_content_rl);
		mBottomLayoutNormal = findViewById(R.id.shopping_cart_bottom_rl);
		mBottomLayoutDelete = findViewById(R.id.shopping_cart_bottom_rl_delete);
		mProgressBar = (ProgressBar)findViewById(R.id.shopping_cart_prgressbar);
		mBtnBottomDelete = (Button)findViewById(R.id.shopping_cart_btn_delete);
		mBtnBack = (Button)findViewById(R.id.shopping_cart_top_arrow_left);
		mBtnCheckAll = (Button)findViewById(R.id.shopping_cart_check_all);
		mBtnAppoinment = (Button)findViewById(R.id.shopping_cart_btn_appointment);
		mBtnSettlement = (Button)findViewById(R.id.shopping_cart_btn_settlement);
		mTvPrompt = (TextView)findViewById(R.id.shopping_cart_tv_prompt);
		mTvTopTitle = (TextView)findViewById(R.id.shopping_cart_top_tv_title);
		mTvTopEdit = (TextView)findViewById(R.id.shopping_cart_top_tv_edit);
		mTvTotalMoney = (TextView)findViewById(R.id.shopping_cart_total_money);
		mTvTotalMoneyWaitWeight = (TextView)findViewById(R.id.shopping_cart_total_money_wait_weight);
		mTvTotalDes = (TextView)findViewById(R.id.shopping_cart_total_des);
		mWeightTag = findViewById(R.id.shopping_cart_weight_tag);
		mPullToRefreshListView = (PullToRefreshListView)findViewById(R.id.shopping_cart_goods_list);
		
		mTvTopEdit.setOnClickListener(mOnClickListener);
		mBtnBottomDelete.setOnClickListener(mOnClickListener);
		mBtnBack.setOnClickListener(mOnClickListener);
		mBtnCheckAll.setOnClickListener(mOnClickListener);
		mBtnAppoinment.setOnClickListener(mOnClickListener);
		mBtnSettlement.setOnClickListener(mOnClickListener);
		mPullToRefreshListView.setOnRefreshListener(this);
		
		mDecimalFormat = new DecimalFormat("#########.##");
		mDataList = new ArrayList<ShoppingCartItemData>();
		mDataListForDelete = new ArrayList<ShoppingCartItemData>();
		mShoppingCartListAdapter = new ShoppingCartListAdapter(this, mDataList, mShoppingCartListAdapterListener);
		mShoppingCartListAdapter.registerDataSetObserver(mDataSetObserver);
		mShoppingCartDeleteListAdapter = new ShoppingCartDeleteListAdapter(this, mDataListForDelete);
		mShoppingCartDeleteListAdapter.registerDataSetObserver(mDataSetObserver);
		mPullToRefreshListView.setAdapter(mShoppingCartListAdapter);
		
		mServerSection = UtilTools.getServerSection(getApplication());
		// open&close time
		mOpenCalendar = Calendar.getInstance();
		mCloseCalendar = Calendar.getInstance();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		this.retrieveStoreInfo();
		this.refreshData();
	}
	
	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		if(isEditMode) {
			this.changeDisplayMode();
		}
		this.refreshData();
	}
	
	private void changeDisplayMode() {
		if(isEditMode) {
			for(ShoppingCartItemData itemData : mDataList) {
    			itemData.isChecked = false;
    		}
			mPullToRefreshListView.setAdapter(mShoppingCartListAdapter);
			
    		isEditMode = false;
    		mBottomLayoutNormal.setVisibility(View.VISIBLE);
    		mBottomLayoutDelete.setVisibility(View.INVISIBLE);
    		mTvTopEdit.setText(getResources().getString(R.string.shopping_cart_edit));
    	}
    	else {
    		mDataListForDelete.clear();
    		for(ShoppingCartItemData itemData : mDataList) {
    			itemData.isChecked = false;
    			mDataListForDelete.add(itemData);
    		}
    		mPullToRefreshListView.setAdapter(mShoppingCartDeleteListAdapter);
    		
    		isEditMode = true;
    		mBottomLayoutNormal.setVisibility(View.INVISIBLE);
    		mBottomLayoutDelete.setVisibility(View.VISIBLE);
    		mTvTopEdit.setText(getResources().getString(R.string.shopping_cart_edit_cancel));
    	}
	}
	
	private void refreshTopCount() {
		if(mDataList != null && mDataList.size() > 0) {
			int count = 0;
			for(ShoppingCartItemData itemData : mDataList) {
				if(itemData.itemType == 0) {
					count++;
				}
			}
			
			String str = getResources().getString(R.string.shopping_cart);
			str = str + "(" + count + ")";
			mTvTopTitle.setText(str);
		}
		else {
			mTvTopTitle.setText(R.string.shopping_cart);
		}
	}
	
	private void retrieveStoreInfo() {
		// 读取当前ServerSection的StoreInfo list
		mCvsStoreInfo = null;
		RetrieveDataAsyn.getInstance().getStoreInfosForStore(-1, new RetrieveDataListener() {
			@Override
			public void dataRetrieved(int requestId, Object data) {
				if(data != null) {
					mCvsStoreInfo = (StoreInfo)data;
				}
				
				if(mCvsStoreInfo != null) {
					mOpenCalendar.setTime(mCvsStoreInfo.getOpenTime());
					mCloseCalendar.setTime(mCvsStoreInfo.getCloseTime());
					Log.d("CEXX", "CEXX > ShoppingCartActivity > mOpenCalendar : " + mOpenCalendar);
					Log.d("CEXX", "CEXX > ShoppingCartActivity > mCloseCalendar : " + mCloseCalendar);
				}
			}
		});
	}
	
	private void refreshData() {
		mTvPrompt.setVisibility(View.GONE);
		mProgressBar.setVisibility(View.VISIBLE);
		RetrieveDataAsyn.getInstance().getShoppingCart(
				AVUser.getCurrentUser().getObjectId(), AntHomeApplication.sectionNum, -1, mRetrieveDataListener);
	}
	
	private void arrangeData(List<ShoppingCart> scList) {
		List<ShoppingCartItemData> tempList = new ArrayList<ShoppingCartItemData>();
		int notOpenType = 1;
		boolean storeOpen = ShoppingCartActivity.this.isOpen();
		if(scList != null) {
			if(!storeOpen) {
				int openHour = mOpenCalendar.get(Calendar.HOUR_OF_DAY);
	    		int openMinute = mOpenCalendar.get(Calendar.MINUTE);
	    		int closeHour = mCloseCalendar.get(Calendar.HOUR_OF_DAY);
	    		int closeMinute = mCloseCalendar.get(Calendar.MINUTE);
	    		
	    		Calendar nowCalendar = Calendar.getInstance();
	    		int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
	    		int nowMinute = nowCalendar.get(Calendar.MINUTE);
	    		
    			if(nowHour < openHour || (nowHour == openHour && nowMinute < openMinute)) {
    				notOpenType = 1;
    			}
    			else {
    				notOpenType = 2;
    			}
			}
	    	
        	for(ShoppingCart sc : scList) {
        		if(sc.getGoods() == null) {
        			continue;
        		}
        		
    			ShoppingCartItemData itemData = new ShoppingCartItemData();
                itemData.mUIShoppingCart = sc;
                itemData.isChecked = false;
                itemData.itemType = 0;
                itemData.isStoreOpen = storeOpen;
                
                if(sc.getGoods().getStock() <= 0 || sc.getGoods().getShowTag() == 3) {
                	itemData.isSoldOut = true;
                }
                else {
                	itemData.isSoldOut = false;
                }
                
                if(sc.getGoods().getIsOnShelf()) {
                	itemData.isOffShelf = false;
                }
                else {
                	itemData.isOffShelf = true;
                }
                
                tempList.add(itemData);
			}
        }
		
		List<ShoppingCartItemData> offShelfList = new ArrayList<ShoppingCartItemData>();
		HashMap<String, List<ShoppingCartItemData>> mapByStoreInfo = new HashMap<String, List<ShoppingCartItemData>>();
		// 分出下架的商品
		for (ShoppingCartItemData itemData : tempList) {
			if (itemData.isOffShelf) {
				offShelfList.add(itemData);
			}
		}
		tempList.removeAll(offShelfList);
		
		// 对不同商铺的商品进行分类
		String storeId = null;
		List<ShoppingCartItemData> scidList = null;
		while(tempList.size() > 0) {
			storeId = tempList.get(0).mUIShoppingCart.getStoreId();
			storeId = (storeId==null ? "" : storeId);
			scidList = new ArrayList<ShoppingCartItemData>();
			
			if(TextUtils.isEmpty(storeId)) {
				for(ShoppingCartItemData scid : tempList) {
					if(TextUtils.isEmpty(scid.mUIShoppingCart.getStoreId())) {
						scidList.add(scid);
					}
				}
			}
			else {
				for(ShoppingCartItemData scid : tempList) {
					if(storeId.equals(scid.mUIShoppingCart.getStoreId())) {
						scidList.add(scid);
					}
				}
			}
			mapByStoreInfo.put(storeId, scidList);
			tempList.removeAll(scidList);
		}
		
		// 如果休市，加入休市提示行
		if(!storeOpen && mapByStoreInfo.size() > 0) {
			ShoppingCartItemData itemData = new ShoppingCartItemData();
	    	itemData.isChecked = false;
	    	itemData.itemType = 1;
	    	itemData.notOpenType = notOpenType;
	    	mDataList.add(itemData);
		}
		
		// 加入普通的商品(按店铺分类)
		Set<String> storeIdList = mapByStoreInfo.keySet();
		for(String id : storeIdList) {
			// 加入店铺名称行
			ShoppingCartItemData itemData = new ShoppingCartItemData();
			itemData.isChecked = false;
			itemData.itemType = 2;
			itemData.storeId = id;
			mDataList.add(itemData);
			// 加入该店铺的商品
			mDataList.addAll(mapByStoreInfo.get(id));
		}
		
		if(offShelfList.size() > 0) {
			// 加入下架的提示行
			ShoppingCartItemData itemData = new ShoppingCartItemData();
	    	itemData.isChecked = false;
	    	itemData.itemType = 3;
	    	mDataList.add(itemData);
	    	// 加入下架商品
	    	mDataList.addAll(offShelfList);
		}
        
        mShoppingCartListAdapter.notifyDataSetInvalidated();
	}
	
	public void refreshDataAfterDelete() {
		List<ShoppingCartItemData> tempList = new ArrayList<ShoppingCartItemData>();
		for(ShoppingCartItemData idata : mDataList) {
			if(idata.itemType == 0) {
				tempList.add(idata);
			}
		}
		
		mDataList.clear();
		int notOpenType = 1;
		boolean storeOpen = ShoppingCartActivity.this.isOpen();
		if(!storeOpen) {
			int openHour = mOpenCalendar.get(Calendar.HOUR_OF_DAY);
    		int openMinute = mOpenCalendar.get(Calendar.MINUTE);
    		int closeHour = mCloseCalendar.get(Calendar.HOUR_OF_DAY);
    		int closeMinute = mCloseCalendar.get(Calendar.MINUTE);
    		
    		Calendar nowCalendar = Calendar.getInstance();
    		int nowHour = nowCalendar.get(Calendar.HOUR_OF_DAY);
    		int nowMinute = nowCalendar.get(Calendar.MINUTE);
    		
			if(nowHour < openHour || (nowHour == openHour && nowMinute < openMinute)) {
				notOpenType = 1;
			}
			else {
				notOpenType = 2;
			}
		}
		
		List<ShoppingCartItemData> offShelfList = new ArrayList<ShoppingCartItemData>();
		HashMap<String, List<ShoppingCartItemData>> mapByStoreInfo = new HashMap<String, List<ShoppingCartItemData>>();
		// 分出下架的商品
		for (ShoppingCartItemData itemData : tempList) {
			if (itemData.isOffShelf) {
				offShelfList.add(itemData);
			}
		}
		tempList.removeAll(offShelfList);
		
		// 对不同商铺的商品进行分类
		String storeId = null;
		List<ShoppingCartItemData> scidList = null;
		while(tempList.size() > 0) {
			storeId = tempList.get(0).mUIShoppingCart.getStoreId();
			storeId = (storeId==null ? "" : storeId);
			scidList = new ArrayList<ShoppingCartItemData>();
			
			if(TextUtils.isEmpty(storeId)) {
				for(ShoppingCartItemData scid : tempList) {
					if(TextUtils.isEmpty(scid.mUIShoppingCart.getStoreId())) {
						scidList.add(scid);
					}
				}
			}
			else {
				for(ShoppingCartItemData scid : tempList) {
					if(storeId.equals(scid.mUIShoppingCart.getStoreId())) {
						scidList.add(scid);
					}
				}
			}
			mapByStoreInfo.put(storeId, scidList);
			tempList.removeAll(scidList);
		}
		
		// 如果休市，加入休市提示行
		if(!storeOpen && mapByStoreInfo.size() > 0) {
			ShoppingCartItemData itemData = new ShoppingCartItemData();
	    	itemData.isChecked = false;
	    	itemData.itemType = 1;
	    	itemData.notOpenType = notOpenType;
	    	mDataList.add(itemData);
		}
		
		// 加入普通的商品(按店铺分类)
		Set<String> storeIdList = mapByStoreInfo.keySet();
		for(String id : storeIdList) {
			// 加入店铺名称行
			ShoppingCartItemData itemData = new ShoppingCartItemData();
			itemData.isChecked = false;
			itemData.itemType = 2;
			itemData.storeId = id;
			mDataList.add(itemData);
			// 加入该店铺的商品
			mDataList.addAll(mapByStoreInfo.get(id));
		}
		
		if(offShelfList.size() > 0) {
			// 加入下架的提示行
			ShoppingCartItemData itemData = new ShoppingCartItemData();
	    	itemData.isChecked = false;
	    	itemData.itemType = 3;
	    	mDataList.add(itemData);
	    	// 加入下架商品
	    	mDataList.addAll(offShelfList);
		}
        
        mDataListForDelete.clear();
        mDataListForDelete.addAll(mDataList);
	}
	
	private ShoppingCartListAdapterListener mShoppingCartListAdapterListener = new ShoppingCartListAdapterListener()
    {
        @Override
        public void allItemChecked(boolean isAllChecked)
        {
            mBtnCheckAll.setSelected(isAllChecked);
            
            boolean haveStoreClose = false;
            for(ShoppingCartItemData itemData : mDataList)
            {
            	if(itemData.itemType != 0 || itemData.isOffShelf) {
    				continue;
    			}
            	
                if(!itemData.isStoreOpen && itemData.isChecked) {
                	haveStoreClose = true;
                }
            }
            
            if(haveStoreClose) {
            	mBtnSettlement.setEnabled(false);
            }
            else {
            	mBtnSettlement.setEnabled(true);
            }
        }

		@Override
		public void totalMoneyChanged(boolean weightable, double totalMoney) {
			Log.d("CEXX", "CEXX ---> totalMoneyChanged ---> weightable : " + weightable);
			Log.d("CEXX", "CEXX ---> totalMoneyChanged ---> totalMoney : " + totalMoney);
			if(weightable) {
				mWeightTag.setVisibility(View.VISIBLE);
				mTvTotalMoney.setVisibility(View.GONE);
				mTvTotalMoneyWaitWeight.setVisibility(View.VISIBLE);
			}
			else {
				mWeightTag.setVisibility(View.GONE);
				mTvTotalMoney.setVisibility(View.VISIBLE);
				mTvTotalMoneyWaitWeight.setVisibility(View.GONE);
				mTvTotalMoney.setText(mDecimalFormat.format(totalMoney));
			}
			
			mTotalMoney = totalMoney;
		}
    };
    
    public void goToOrderCreateActivity(Date sendDate) {
    	ArrayList<ShoppingCartItemData> itemDataList = new ArrayList<ShoppingCartItemData>();
    	for(ShoppingCartItemData itemData : mDataList) {
    		if(!itemData.isOffShelf && itemData.itemType == 0 && itemData.isChecked) {
    			if(itemData.isSoldOut) {
        			UtilTools.showMyToast(this, getResources().getString(R.string.shopping_cart_choose_sold_out_goods));
        			return;
        		}
    			
    			itemDataList.add(itemData);
    		}
    	}
    	// check choosed goods or not
    	if(itemDataList.size() == 0) {
    		UtilTools.showMyToast(this, 
    				getResources().getString(R.string.shopping_cart_please_choose_goods_first));
    		return;
    	}
    	// check store open or not
    	for(ShoppingCartItemData itemData : itemDataList) {
    		if(itemData.itemType != 0 || itemData.isOffShelf) {
				continue;
			}
    		
    		if(!itemData.isStoreOpen) {
    			UtilTools.showMyToast(this, 
        				getResources().getString(R.string.shopping_cart_goods_partly_store_close));
    			break;
    		}
    	}
    	
    	ArrayList<ShoppingCart> uscList = new ArrayList<ShoppingCart>();
    	for(ShoppingCartItemData itemData : itemDataList) {
    		uscList.add(itemData.mUIShoppingCart);
    	}
    	
    	Intent intent = new Intent(this, OrderCreateActivity.class);
    	if(sendDate != null) {
    		intent.putExtra(OrderCreateActivity.KeySendTime, sendDate);
    	}
    	Const.mShoppingCartList = uscList;
    	intent.putExtra(OrderCreateActivity.KeyGoodsList, true);
    	this.startActivity(intent);
    }
    
    public boolean isOpen() {
		SimpleDateFormat sdf = new SimpleDateFormat("HHmm");
		Date date = new Date();
		date.setTime(System.currentTimeMillis());
		String current = sdf.format(date);
		String open = sdf.format(new Date(mOpenCalendar.getTimeInMillis()));
		String close = sdf.format(new Date(mCloseCalendar.getTimeInMillis()));

		double currentTime = Double.parseDouble(current);
		double openTime = Double.parseDouble(open);
		double closeTime = Double.parseDouble(close);

		if (currentTime > openTime && currentTime < closeTime) {
			return true;
		}
		return false;
	}
}
